set more off
capture log close

//  program:    AnanatEtAl_test_score_results
//  task:		Run Regressions for Figure 2 test score results

//change working directory to point to the file with the data
cd *** Fill in path name here ***

//create log file
log using AnanatEtAl_test_score_results, replace text

//load the merged bls naep data
use AnanatEtAl_test_score_data, clear

//set the panel variables
xtset stateno year

//run regressions using test-taker sample sizes as frequency weights

** All Students **
ivregress 2sls z_all yrd* stated* (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store ALL
outreg2 [ALL] using fig2_test_scores, drop(yrd* stated*) excel replace dec(3)

** Black Students **
ivregress 2sls z_black yrd* stated* (seppctyrlag = ticpctyrlag) [fweight=n_black], vce(cluster stateno) first
est store BLACK
outreg2 [BLACK] using fig2_test_scores, drop(yrd* stated*) excel dec(3) 

** White Students **
ivregress 2sls z_white yrd* stated* (seppctyrlag = ticpctyrlag) [fweight=n_white], vce(cluster stateno) first
est store WHITE
outreg2 [WHITE] using fig2_test_scores, drop(yrd* stated*) excel dec(3)

clear

** State-years with below median unemployment **
//load the data
use AnanatEtAl_test_score_data, clear
//set the panel variables
xtset stateno year
//generate the median unemployment cutoff
sort year
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag<=cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store LOWUNEMP
outreg2 [LOWUNEMP] using fig2_test_scores, drop(yrd* stated*) excel dec(3)

clear all

** State-years with above median unemployment **
//load the data
use AnanatEtAl_test_score_data, clear
//set the panel variables
xtset stateno year
//generate the median unemployment cutoff value
sort year
by year: egen cutoff = pctile(unemplag), p(50)
keep if unemplag>cutoff
//run regression weighted by test-taker sample size
ivregress 2sls z_all yrd* stated* (seppctyrlag = ticpctyrlag) [fweight=sample_size], vce(cluster stateno) first
est store HIGHUNEMP
outreg2 [HIGHUNEMP] using fig2_test_scores, drop(yrd* stated*) excel dec(3)

clear all

log close
